package com.cs543.j.client.cloudService.storage;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.cs543.j.client.cloudService.filesystem.Directory;
import com.cs543.j.client.cloudService.filesystem.Entry;

public class JSON2Entries implements OnRPCFinishListener {

	Directory parent;
	OnLookupResponceListener listener;

	public JSON2Entries(Directory parent, OnLookupResponceListener listener) {
		super();
		this.parent = parent;
		this.listener = listener;
	}

	public void onFinish(Object result) {

		if (result != null && result instanceof JSONArray) {
			JSONArray array = (JSONArray) result;
			Entry[] list = new Entry[array.length()];

			for (int i = 0; i < list.length; i++) {
				try {
					list[i] = Entry.getInstanceFromJSON((JSONObject) array.get(i), this.parent);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			this.listener.onLookupResponce(list);
		}
		else
		{
			this.listener.onLookupResponce(null);
		}
	}

}
